package com.medical.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.system.common.Result;
import com.medical.system.model.Client;
import com.medical.system.service.SysClientService;
import com.medical.system.util.MD5Helper;
import com.medical.system.vo.ClientVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RestController
@Api(tags = "患者控制器")
@RequestMapping("admin/system/client")
public class SysClientController {
        @Autowired
        private SysClientService sysClientService;

     @ApiOperation("分页和条件查询")
     @GetMapping("/{page}/{limit}")
      public Result selectClientPageByVo(@PathVariable Long page,
                                   @PathVariable Long limit,
                                   ClientVo clientVo){
         IPage<Client> iPage = new Page<>(page,limit);

         iPage =   this.sysClientService.selectPage(iPage,clientVo);
         return Result.ok(iPage);
     }

    @ApiOperation("更改患者状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status)
    {
        this.sysClientService.updateStatus(id,status);
        return Result.ok();
    }

    @ApiOperation("根据ID查询患者")
    @GetMapping("/findClientById/{id}")
    public Result queryRoleById(@PathVariable Long id) {
        Client client = this.sysClientService.getById(id);
        return Result.ok(client);
    }

    @ApiOperation("修改患者")
    @PostMapping("updateClient")
    public Result updateRole(@RequestBody Client client) {
        boolean isSuccess = this.sysClientService.updateById(client);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("添加新患者")
    @PostMapping("addClient")
    public Result insertUser(@RequestBody Client client) {
//        加密密码
        String encrypt = MD5Helper.encrypt(client.getPassword());
        Random random = new Random();
        int visitCode = random.nextInt(999999);
        String s = Integer.toString(visitCode);
        client.setVisitCode(s);
        client.setPassword(encrypt);
        boolean saved = this.sysClientService.save(client);
        if (saved) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("删除患者")
    @DeleteMapping("/removeClintById/{id}")
    public Result removeUser(@PathVariable Long id) {
        boolean removed = this.sysClientService.removeById(id);
        if (removed) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

}
